package peer.tools;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public class Setting {
	File f;
	int ver=1;
	HashMap<String, String> hm;
	public Setting (File f){
	hm=new HashMap<String, String>();
	this.f=f;
	}
	
	public void creat(){
		try {
			f.createNewFile();
			DataOutputStream dos=new DataOutputStream(new FileOutputStream(f));
			dos.writeInt(ver);
			dos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		load();
	}
	public void  load() {
		if(!f.exists()){
			try {
				f.createNewFile();
				DataOutputStream dos=new DataOutputStream(new FileOutputStream(f));
				dos.writeInt(ver);
				dos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		if(f.length()>=6)
		{			
			try {
				hm=new HashMap<String, String>();
				DataInputStream dis=new DataInputStream(new FileInputStream(f));
				@SuppressWarnings("unused")
				int ver=dis.readInt();
				int num=dis.readInt();
				String s1;
				String s2;
				byte s1b[];
				byte s2b[];
				for(int i=0;i<num;i++){	
					int j=dis.readInt();			
					s1b=new byte[j];			
					dis.read(s1b);		
					s1=new String(s1b);	
					int k=dis.readInt();
					s2b=new byte[k];
					dis.read(s2b);
					s2=new String(s2b);
					hm.put(s1,s2);
					String ss=new String (s1b);
					System.out.println(hm.containsKey(ss));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void save(){
	    Iterator<String> it=hm.keySet().iterator();
	    int num=hm.size();
	    DataOutputStream dos;
		try {
			dos = new DataOutputStream(new FileOutputStream(f));
			dos.writeInt(ver);	    
		    String[] s1=new String[num];
		    int[] s1l=new int[num];
		    String[] s2=new String[num];
		    int[] s2l=new int[num];
		    int i=0;
			while(it.hasNext()){
			    s1[i]=it.next();
			    s2[i]=hm.get(s1[i]);
			    s1l[i]=s1[i].getBytes().length;
			    s2l[i]=s2[i].getBytes().length;
			    i++;
			}
			dos.writeInt(num);
			int j=0;
			while(j<num){
				byte[] s1b;
				byte[] s2b;
				dos.writeInt(s1l[j]);
				s1b=s1[j].getBytes();
				dos.write(s1b);
				dos.writeInt(s2l[j]);
				s2b=s2[j].getBytes();
				dos.write(s2b);	
				j++;
			}
			dos.flush();
			dos.close();	
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	 
	}	
	
	public int size(){		
		return hm.size();
	}
	
	public boolean contains(String str){		
			return hm.containsKey(str);
	}
	
	public boolean containsKey(String str){		
		return hm.containsKey(str);
	}
	
	public String get(String str){
		return hm.get(str);
	}
	
	public  void add(String s1,String s2){
		hm.put(s1,s2);
		save();
	}
	
	public void remove(String s1){
		hm.remove(s1);
		save();
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<String, String> getHm(){
		return (HashMap<String, String>)hm.clone();
	}
}
